﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace CodeSystem.Tool.Helpers
{
    /// <summary>
    /// HTTP 请求助手类。
    /// </summary>
    public class HttpHelper
    {
        private readonly HttpClient _httpClient;

        /// <summary>
        /// 初始化 HttpHelper。
        /// </summary>
        /// <param name="baseAddress">API 的基础地址。</param>
        public HttpHelper(string baseAddress)
        {
            _httpClient = new HttpClient
            {
                BaseAddress = new Uri(baseAddress)
            };
            _httpClient.DefaultRequestHeaders.Accept.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        /// <summary>
        /// 发送 GET 请求，返回 HTML 内容。
        /// </summary>
        /// <param name="url">请求的 URL。</param>
        /// <returns>返回 HTML 字符串。</returns>
        public async Task<string> GetHtmlAsync(string url)
        {
            var response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode(); // 确保请求成功
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送 POST 请求，提交 HTML 表单数据。
        /// </summary>
        /// <param name="url">请求的 URL。</param>
        /// <param name="formData">表单数据（键值对）。</param>
        /// <returns>返回 HTML 字符串。</returns>
        public async Task<string> PostHtmlFormAsync(string url, FormUrlEncodedContent formData)
        {
            var response = await _httpClient.PostAsync(url, formData);
            response.EnsureSuccessStatusCode(); // 确保请求成功
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送 POST 请求，提交 HTML 内容。
        /// </summary>
        /// <param name="url">请求的 URL。</param>
        /// <param name="htmlContent">HTML 内容。</param>
        /// <returns>返回 HTML 字符串。</returns>
        public async Task<string> PostHtmlAsync(string url, string htmlContent)
        {
            var content = new StringContent(htmlContent, Encoding.UTF8, "text/html");
            var response = await _httpClient.PostAsync(url, content);
            response.EnsureSuccessStatusCode(); // 确保请求成功
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 设置请求头。
        /// </summary>
        /// <param name="name">请求头名称。</param>
        /// <param name="value">请求头值。</param>
        public void SetHeader(string name, string value)
        {
            _httpClient.DefaultRequestHeaders.Add(name, value);
        }

        /// <summary>
        /// 设置授权令牌。
        /// </summary>
        /// <param name="token">授权令牌。</param>
        public void SetBearerToken(string token)
        {
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        }
    }
}
